package com.icen.utils.timer;

import java.util.Timer;
import java.util.TimerTask;

public class APPTimerSchedule extends Timer {
	// 延时:多长时间后开始执行(毫秒)
	public long DELAY = 500;
	// 周期:多长时间执行一次(秒)
	public long PERIOD = 25;
	
	private static APPTimerSchedule mTimer = null;
	private GisLocTimerTask mTimerTask = null;
	
	// 获取实例
	public static APPTimerSchedule getInstance(){
		if(mTimer == null){
			mTimer = new APPTimerSchedule();
		}
		return mTimer;
	}
	
	// 执行计时任务
	public void startLocTask(){
		// 关闭旧的任务
		this.cancelGisTimerTask();
		mTimerTask = new GisLocTimerTask();
		if(mTimer != null){
			mTimer.schedule(mTimerTask, DELAY, PERIOD * 1000);
		}
	}
	
	// 计时任务
	class GisLocTimerTask extends TimerTask{
		@Override
		public void run() {
			// 定时下载车辆信息
			//MainDao.downloadCarInfoByCarCode(MainActivity.mCurrentCarInfo.getCarCode());
			//DownloadController.downloadCarInfoByCarCode(MainActivity.mCurrentCarInfo.getCarCode(), DownloadController.DOWN_TYPE_LOC);
		}
	}

	// 取消计时任务
	public void cancelTimer(){
		this.cancelGisTimerTask();
		this.cancelGisTimer();
	}
	
	// 取消计时
	private void cancelGisTimer(){
		if(mTimer != null){
			mTimer.cancel();
			mTimer = null;
		}
	}
	
	// 取消任务
	private void cancelGisTimerTask(){
		if(mTimerTask != null && !mTimerTask.cancel()){
			mTimerTask.cancel();
			mTimerTask = null;
		}
	}
}
